package cn.edu.jlu.community.disuse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;

import java.util.Map;

import cn.edu.jlu.community.R;

public class ForumActivityOld extends Activity {

    public void post(String title, String content) {
        AVObject post = new AVObject("Post");
        post.put("title", title);
        post.put("content", content);
        post.put("writer", AVUser.getCurrentUser().getUsername());
        post.put("category", 1);   // BBS category .
        post.put("commentCount", 0);
        post.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Log.i("aaa", "post succeeded");
                    Toast.makeText(ForumActivityOld.this, "post completed", Toast.LENGTH_LONG).show();
                    EditText ed = (EditText) findViewById(R.id.bbs_content_editText);
                    ed.setText("");
                    ed = (EditText) findViewById(R.id.bbs_title_editText);
                    ed.setText("");

                    Intent intent = new Intent("android.intent.action.REFRESH");
                    sendBroadcast(intent);
                } else {
                    Log.i("aaa", "post : something wrong" + e.getMessage());
                    Toast.makeText(ForumActivityOld.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbs);


        findViewById(R.id.bbs_post_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText titleEditText = (EditText) findViewById(R.id.bbs_title_editText);
                String title = titleEditText.getText().toString();
                EditText contentEditText = (EditText) findViewById(R.id.bbs_content_editText);
                String content = contentEditText.getText().toString();
                Log.i("aaa", title);
                Log.i("aaa", content);
                post(title, content);
            }
        });

        findViewById(R.id.bbs_refresh_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("aaa","refresh request sent") ;
                Intent intent = new Intent("android.intent.action.REFRESH");
                sendBroadcast(intent);
            }
        });

        ListView listView = (ListView) findViewById(R.id.bbs_listView);
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, DataLoader.bbs_list, R.layout.post_list_item,
                new String[]{"title", "update_time", "author", "comment_count"},
                new int[]{R.id.post_title, R.id.post_time,
                        R.id.post_author, R.id.post_comment_number});
        simpleAdapter.notifyDataSetChanged();

        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String, Object> map = DataLoader.bbs_list.get(i) ;

                Intent intent = new Intent(ForumActivityOld.this , PostContentActivity.class) ;
                intent.putExtra("objID",(String)map.get("objID")) ;
                intent.putExtra("title",(String)map.get("title")) ;
                intent.putExtra("author",(String)map.get("author")) ;
                intent.putExtra("content",(String)map.get("content")) ;

                ForumActivityOld.this.startActivity(intent);
            }
        });
    }
}
